using System.Threading.Tasks;
using Comteck.Dto.Users;
using Comteck.Entities.Users;
using Comteck.Ktt.Services.Infrastructure;
using ExpressMapper.Extensions;

namespace Comteck.Ktt.Services.Users {
  /// <summary>
  /// 
  /// </summary>
  public class UserService : BaseKttService, IUserService {
    private readonly IKttRepository<User, string> _userRepository;

    public UserService(IKttUnitOfWork unitOfWork) : base(unitOfWork) {
      _userRepository = _unitOfWork.GetRepository<User, string>();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="userDto"></param>
    /// <returns></returns>
    public async Task<AjaxResponse<UserDto>> AuthenticateAsync(LoginUserDto userDto) {
      var result = new AjaxResponse<UserDto>() {
        Code = 0,
      };

      var user = await _userRepository.GetByIdAsync(userDto.UserId);
      if (user == null) {
        result.Message = "用户不存在";
        return result;
      }

      if (userDto.Password != user.Password) {
        result.Message = "密码不正确";
        return result;
      }

      result.Data = user.MapTo<UserDto>();
      result.Code = 1;
      return result;
    }
  }

}
